package online.westonline.com.westonline.adapter.constacts;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.easy.west.westclient.api.department.bean.AllDepartMentBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.entry.EmmmBean;
import online.westonline.com.westonline.wedgit.DownImgView;

/**
 * 通讯录适配器
 * Created by xiatao on 2018/8/15.
 */

public class DepartMentAndConstactsAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<AllDepartMentBean.DataBean> departMentBeanList = new ArrayList<>();
    private int NORMAL_TYPE = 1;
    private int EMPTY_TYPE = 0;

    public void setData(List<AllDepartMentBean.DataBean> departMentBeanList) {
        this.departMentBeanList = departMentBeanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == NORMAL_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.department_item, parent, false);
            return new DepartMentViewHolder(view);
        } else if (viewType == EMPTY_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false);
            return new EmptyViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof EmptyViewHolder)
        {
            EmptyViewHolder viewHolder = (EmptyViewHolder) holder;
            viewHolder.tvEmptyDesc.setText("没有部门");
        }
        if (holder instanceof DepartMentViewHolder)
        {
            final AllDepartMentBean.DataBean dataBean = departMentBeanList.get(position);
            final DepartMentViewHolder viewHolder = (DepartMentViewHolder) holder;
            viewHolder.tvState.setVisibility(View.GONE);
            viewHolder.tvDepartmentTitle.setText(dataBean.getName());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (viewHolder.imgExpand.isShow())
                    {
                        viewHolder.imgExpand.show(false);
                        viewHolder.recyclerViewChildPeople.setVisibility(View.GONE);
                    }
                    else
                    {
                        //展开数据
                        viewHolder.imgExpand.show(true);
                        viewHolder.setDatas(dataBean.getDepartmentDTOS(),viewHolder.recyclerViewChildPeople);
                    }
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (departMentBeanList.size() == 0) {
            return EMPTY_TYPE;
        }
        return NORMAL_TYPE;
    }

    @Override
    public int getItemCount() {
        return departMentBeanList == null ? 0 : departMentBeanList.size();
    }

    /***
     * 部门适配器
     */
    class DepartMentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_expand)
        DownImgView imgExpand;
        @BindView(R.id.tv_department_title)
        TextView tvDepartmentTitle;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.recyclerView_child_people)
        RecyclerView recyclerViewChildPeople;
        @BindView(R.id.pro_constacts_state)
        ProgressBar proConstactsState;

        public DepartMentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        /***
         * 设置二级菜单数据
         * @param departmentDTOS
         * @param recyclerViewChildPeople
         */
        public void setDatas(List<AllDepartMentBean.DataBean.DepartmentDTOSBean> departmentDTOS, RecyclerView recyclerViewChildPeople) {
            ConstactsAdapter constactsAdapter = new ConstactsAdapter();
            EmmmBean emmmBean = new EmmmBean();
            emmmBean.setDepartMentBeanList(departmentDTOS);
            constactsAdapter.setDatas(emmmBean);
            recyclerViewChildPeople.setVisibility(View.VISIBLE);
            recyclerViewChildPeople.setLayoutManager(new LinearLayoutManager(this.itemView.getContext()));
            recyclerViewChildPeople.setAdapter(constactsAdapter);
            constactsAdapter.notifyDataSetChanged();
        }
    }

    /***
     * 空布局展开
     */
    class EmptyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_empty_desc)
        TextView tvEmptyDesc;
        public EmptyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
